/**
 * Base64Tests
 *
 * @author Chris Pratt
 *
 * 12/18/2008
 */
package com.anodyzed.onyx.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import static org.junit.Assert.*;
import org.junit.Test;

public class Base64Tests {
  private static final String testee = "The quick brown fox jumps over the lazy dog.";
  private static final String testicle = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=";

  /**
   * Test tye Byte Array methods
   */
  @Test public void testBytes () {
    String encoded = Base64.encode(testee.getBytes());
    assertEquals(testicle,encoded);
    assertArrayEquals(testee.getBytes(),Base64.decode(encoded));
  } //testBytes

  /**
   * Test the Character Stream based methods
   */
  @Test public void testReader () throws IOException {
    StringWriter encoded = new StringWriter();
    Base64.encode(new ByteArrayInputStream(testee.getBytes()),encoded);
    assertEquals(testicle,encoded.toString());
    ByteArrayOutputStream decoded = new ByteArrayOutputStream();
    Base64.decode(new StringReader(encoded.toString()),decoded);
    assertArrayEquals(testee.getBytes(),decoded.toByteArray());
  } //testReader

  /**
   * Test the Character Array methods
   */
  @Test public void testChars () {
    char[] encoded = Base64.encode(testee.toCharArray());
    assertArrayEquals(testicle.toCharArray(),encoded);
    assertArrayEquals(testee.toCharArray(),Base64.decode(encoded));
  } //testChars

  /**
   * Test the Byte Stream based methods
   */
  @Test public void testStream () throws IOException {
    ByteArrayOutputStream encoded = new ByteArrayOutputStream();
    Base64.encode(new ByteArrayInputStream(testee.getBytes()),encoded);
    assertArrayEquals(testicle.getBytes(),encoded.toByteArray());
    ByteArrayOutputStream decoded = new ByteArrayOutputStream();
    Base64.decode(new ByteArrayInputStream(encoded.toByteArray()),decoded);
    assertArrayEquals(testee.getBytes(),decoded.toByteArray());
  } //testStream

} //*Base64Tests
